package com.acme.bankapp.service.bank;

import com.acme.bankapp.domain.bank.accounts.Account;
import com.acme.bankapp.domain.bank.Bank;
import com.acme.bankapp.domain.bank.Client;

import java.io.*;

/**
 * Created by Student on 14.11.2014.
 */
public class BankService {

    public static String getBalance(Bank bank) {
        StringBuffer result = new StringBuffer();

        for (Client client : bank.getClients()) {
            result.append(client.toString()).append('\n');
        }
        return result.toString();
    }

    public static void printBalance(Bank bank) {
        System.out.println("Balance");
        System.out.println(getBalance(bank));
    }


    public static void printMaximumAmountToWithdraw(Bank bank) {

        System.out.println("Maximum Amount To Withdraw");
        for (Client client : bank.getClients()) {
            System.out.println(client.getName());
            for (Account account : client.getAccounts()) {
                System.out.println(account.maximumAmountToWithdraw());
            }
        }
    }

    public static void saveBank(Bank bank, String path) {
        try (
                ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(new File(path)));
                ) {
            out.writeObject(bank);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Bank readBank(String path) {
        Bank bank = null;
        try (
                ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File(path)));
                ) {
            bank = (Bank) in.readObject();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return bank;
    }
}
